package br.net.woodstock.tickettoride.spring.web.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;

import br.net.woodstock.tickettoride.api.access.UserService;
import br.net.woodstock.tickettoride.orm.User;
import br.net.woodstock.tickettoride.web.jsf.security.common.AbstractLoginBean;

@Controller("loginBean")
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class LoginBean extends AbstractLoginBean {

	private static final long	serialVersionUID	= -3125757940452936893L;

	@Autowired(required = true)
	private UserService			userService;

	@Override
	public UserService getUserService() {
		return this.userService;
	}

	@Override
	public void onLogin(final User user) {
		SecurityContextHolder.getContext().setAuthentication(new TicketAuthentication(user));
	}

	@Override
	public void onLogout(final User user) {
		SecurityContextHolder.getContext().setAuthentication(null);
	}

}
